package com.woniuxy.goods.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.woniuxy.core.annotation.Permission;
import com.woniuxy.core.exception.ParamException;
import com.woniuxy.core.util.Result;
import com.woniuxy.goods.dto.BrandDto;
import com.woniuxy.goods.entity.AttributeInfo;
import com.woniuxy.goods.entity.Brand;
import com.woniuxy.goods.form.AddBrandForm;
import com.woniuxy.goods.form.FindBrandsForm;
import com.woniuxy.goods.form.UpdateBrandForm;
import com.woniuxy.goods.param.AddBrandParam;
import com.woniuxy.goods.param.FindBrandsParam;
import com.woniuxy.goods.param.UpdateBrandParam;
import com.woniuxy.goods.service.BrandService;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *  品牌管理接口
 * @author ...
 * @since 2024年05月22日
 */
@RestController
@Api(tags = "品牌管理接口")
@RequestMapping("/brand")
public class BrandController {
    /**
     * 品牌管理服务层接口
     */
    @Resource
    private BrandService brandService;

    /**
     * 新增品牌
     * @param form 新增品牌对象
     * @return 品牌信息
     * @throws Exception
     */
    @ApiOperation("新增品牌接口")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = Brand.class)
    })
    @PostMapping("/addBrand")
    @Permission(roles = "超级管理员,管理员")
    public Result addBrand(@Validated @RequestBody AddBrandForm form, BindingResult br)throws Exception{
        if(br.hasErrors()) throw new ParamException(10001,"参数有误");
        Brand brand = brandService.addBrand(BeanUtil.toBean(form, AddBrandParam.class));
        return Result.success("新增品牌成功",brand);
    }

    /**
     * 修改品牌
     * @param form 修改品牌对象
     * @return 品牌信息
     * @throws Exception
     */
    @ApiOperation("修改品牌接口")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = Brand.class)
    })
    @PostMapping("/updateBrand")
    @Permission(roles = "超级管理员,管理员")
    public Result updateBrand( @Validated @RequestBody UpdateBrandForm form,BindingResult br)throws Exception{
        if(br.hasErrors()) throw new ParamException(10001,"参数有误");
        Brand brand = brandService.updateBrand(BeanUtil.toBean(form, UpdateBrandParam.class));
        return Result.success("修改品牌成功",brand);
    }

    /**
     * 条件查询品牌
     * @param form 条件查询品牌对象
     * @return 分页品牌信息
     * @throws Exception
     */
    @ApiOperation("条件查询品牌接口")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = BrandDto.class)
    })
    @PostMapping("/findBrands")
    @Permission(roles = "超级管理员,管理员")
    public Result findBrands(@RequestBody FindBrandsForm form)throws Exception{
        IPage<BrandDto> ipage = brandService.findBrands(BeanUtil.toBean(form, FindBrandsParam.class));
        return Result.success("条件查询品牌成功",ipage);
    }

    /**
     * 通过id查询品牌
     * @param brandId 品牌id
     * @return 品牌信息
     * @throws Exception
     */
    @ApiOperation("通过id查询品牌接口")
    @ApiImplicitParam("品牌id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = Brand.class)
    })
    @GetMapping("/findBrandById")
    public Result findBrandById(Integer brandId)throws Exception{
        Brand brand = brandService.findBrandById(brandId);
        return Result.success("通过id查询品牌成功",brand);
    }

    /**
     * 通过ids查询品牌
     * @param brandIds 品牌id集合
     * @return 品牌信息集合
     * @throws Exception
     */
    @ApiOperation("通过ids查询品牌接口")
    @ApiImplicitParam("品牌id集合")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = Brand.class)
    })
    @PostMapping("/findBrandByIds")
    public Result findBrandByIds(@RequestBody Set<Integer> brandIds)throws Exception{
        List<Brand> brands = brandService.findBrandByIds(brandIds);
        return Result.success("通过ids查询品牌成功",brands);
    }

    /**
     * 查询所有品牌
     * @return 品牌信息集合
     * @throws Exception
     */
    @ApiOperation("查询所有品牌")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = Brand.class)
    })
    @GetMapping("/findAllBrands")
    public Result findAllBrands()throws Exception{
        List<Brand> list = brandService.list();
        return Result.success("查询所有品牌成功",list);
    }

}

